﻿using System.Windows.Controls;
using System.Globalization;

namespace Sicily.Wpf.Validators
{
	public class StringRangeValidationRule : ValidationRule
	{
		private int _minimumLength = -1;
		private int _maximumLength = -1;
		private string _errorMessage;

		public int MinimumLength
		{
			get { return _minimumLength; }
			set { _minimumLength = value; }
		}

		public int MaximumLength
		{
			get { return _maximumLength; }
			set { _maximumLength = value; }
		}

		public string ErrorMessage
		{
			get { return _errorMessage; }
			set { _errorMessage = value; }
		}

		public override ValidationResult Validate(object value,
			CultureInfo cultureInfo)
		{
			ValidationResult result = new ValidationResult(true, null);
			string inputString = (value ?? string.Empty).ToString();
			if (inputString.Length < this.MinimumLength ||
				   (this.MaximumLength > 0 &&
					inputString.Length > this.MaximumLength))
			{
				result = new ValidationResult(false, this.ErrorMessage);
			}
			return result;
		}
	}
}
